package com.huawei.appgallery.parcelable.impl;

import android.os.Parcel;
import android.os.Parcelable;
import android.os.Parcelable.Creator;
import com.huawei.appgallery.parcelable.AutoParcelable;
import java.lang.reflect.Field;
import java.util.Map;
import o.cqf;
import o.cqh;
import o.cqj;
import o.cqz;

public class ParcelableArrayTypeProcess implements cqj<Parcelable[]> {

    public static class CreatorExecption extends RuntimeException {
        public CreatorExecption(String str) {
            super(str);
        }
    }

    public /* synthetic */ void ˊ(Parcel parcel, Field field, int i, Object obj, int i2, boolean z) {
        ˏ(parcel, field, i, (Parcelable[]) obj, i2, z);
    }

    public void ˏ(Parcel parcel, Field field, int i, Parcelable[] parcelableArr, int i2, boolean z) {
        if (parcelableArr != null) {
            int ॱ = cqh.ॱ(parcel, i);
            parcel.writeInt(parcelableArr.length);
            for (Parcelable parcelable : parcelableArr) {
                if (parcelable == null) {
                    parcel.writeInt(0);
                } else {
                    ˋ(parcel, parcelable, i2);
                }
            }
            cqh.ˏ(parcel, ॱ);
        } else if (z) {
            cqh.ˎ(parcel, i, 0);
        }
    }

    public void ˋ(AutoParcelable autoParcelable, Field field, Parcel parcel, int i, Map<String, String> map) throws IllegalAccessException {
        field.set(autoParcelable, ˊ(parcel, i, field));
    }

    private <T extends Parcelable> T[] ˊ(Parcel parcel, int i, Field field) {
        int ˋ = cqf.ˋ(parcel, i);
        int dataPosition = parcel.dataPosition();
        if (ˋ == 0) {
            return null;
        }
        T[] tArr;
        try {
            Creator ˋ2 = ˋ(field, null);
            if (ˋ2 != null) {
                tArr = (Parcelable[]) parcel.createTypedArray(ˋ2);
                parcel.setDataPosition(dataPosition + ˋ);
                return tArr;
            }
        } catch (Exception e) {
            cqz.ˏ.ˎ("ParcelableArrayTypeProcess", "error readParcelableArray:" + e.getMessage());
        }
        tArr = null;
        parcel.setDataPosition(dataPosition + ˋ);
        return tArr;
    }

    public static Creator ˋ(Field field, Map<String, String> map) {
        Class type = field.getType();
        if (type.isArray()) {
            type = type.getComponentType();
        }
        if (type == null || !Parcelable.class.isAssignableFrom(type)) {
            return null;
        }
        Class cls;
        String name = field.getName();
        if (!(map == null || map.get(name) == null)) {
            name = (String) map.get(name);
            if (name != null) {
                try {
                    cls = Class.forName(name);
                    return ˊ(cls);
                } catch (ClassNotFoundException e) {
                    cqz.ˏ.ॱ("ParcelableArrayTypeProcess", "error clazz:" + name);
                    return null;
                }
            }
        }
        cls = type;
        return ˊ(cls);
    }

    public static <T extends Parcelable> void ˋ(Parcel parcel, T t, int i) {
        int dataPosition = parcel.dataPosition();
        parcel.writeInt(1);
        int dataPosition2 = parcel.dataPosition();
        t.writeToParcel(parcel, i);
        int dataPosition3 = parcel.dataPosition();
        parcel.setDataPosition(dataPosition);
        parcel.writeInt(dataPosition3 - dataPosition2);
        parcel.setDataPosition(dataPosition3);
    }

    public static Creator ˊ(Class cls) throws CreatorExecption {
        try {
            return (Creator) cls.getDeclaredField("CREATOR").get(null);
        } catch (NoSuchFieldException e) {
            throw new CreatorExecption(cls + " is an Parcelable without CREATOR");
        } catch (IllegalArgumentException e2) {
            throw new CreatorExecption(cls + " IllegalArgumentException");
        } catch (IllegalAccessException e3) {
            throw new CreatorExecption(cls + " IllegalAccessException");
        }
    }
}
